package it.unisa.services;

import it.unisa.beans.Petroglyph;
import it.unisa.beans.Photo;
import it.unisa.dao.PetroglyphDao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * TODO metodi richiamabili solo da archeologo, con AOP (add, remove, e update)
 * TODO Link and unlink  photo --> petroglyph
 * 
 * @author Alfonso
 *
 */
public class PetrogliphWS {
	
	public it.unisa.beanService.Petroglyph[] viewPetroglyphs(float positionX, float positionY, int round)
	{
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PetroglyphDao petro=(PetroglyphDao)appContext.getBean("petroglyphDao");
		ArrayList<Petroglyph> petroList = (ArrayList<Petroglyph>) petro.findAllPetroglyphs();
		
		ArrayList<it.unisa.beanService.Petroglyph> petroNear = new ArrayList<it.unisa.beanService.Petroglyph>();

		for(int i=0; i<petroList.size(); i++) {
		
			if(near(positionX, positionY, petroList.get(i).getPositionX(),petroList.get(i).getPositionY(),round)){
				it.unisa.beanService.Petroglyph petrog = new it.unisa.beanService.Petroglyph();
				petrog.setAccount(petroList.get(i).getAccount().getSsn());
				petrog.setDate(petroList.get(i).getDate().toString());
				petrog.setDescription(petroList.get(i).getDescription());
				petrog.setHour(petroList.get(i).getHour().toString());
				petrog.setId(petroList.get(i).getId());
				petrog.setPositionX(petroList.get(i).getPositionX());
				petrog.setPositionY(petroList.get(i).getPositionY());
				petrog.setTitle(petroList.get(i).getTitle());
				int[] photos = new int[petroList.get(i).getPhotos().size()];
				int j = 0;
				for(Photo ph : petroList.get(i).getPhotos()) {
					photos[j] = ph.getId();
					j++;
				}
				petrog.setPhotos(photos);
				petroNear.add(petrog);
			}
		}
		it.unisa.beanService.Petroglyph[] ret = new it.unisa.beanService.Petroglyph[petroNear.size()];
		for(int i=0; i<petroNear.size(); i++) {
			ret[i] = petroNear.get(i);
		}
		return ret;

	}
	
	private boolean near(float positionAX, float positionAY,  float positionBX, float positionBY, int round) {
		float ah = positionBX - positionAX;
		float bh = positionBY - positionAY;
		float d = (float) Math.sqrt(ah*ah + bh*bh);
		if(d<=round) return true;
		return false;
	}
	
	public boolean removePetrolyph(Petroglyph petroglyph){
		return true;
	}
	
	public boolean updatePetroglyph(Petroglyph petroglyph){

		return true;
	}
	
	public boolean addPhotoToPetroglyph(Photo photo,Petroglyph petroglyph){
		return true;
	}

	public boolean removePhotoToPetroglyph(Photo photo,Petroglyph petroglyph){
		return true;
	}
	
	public boolean addPetroglyph(Petroglyph p) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PetroglyphDao petro=(PetroglyphDao)appContext.getBean("petroglyphDao");
		petro.insert(p);
		return true;
	}
	
}	
